package com.group02.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

;
import com.group02.community.entity.community.ZyCommunity;
import com.group02.community.entity.vo.ZyCommunityVO;
import com.group02.community.service.CommunityService;
import com.group02.community.utils.EasyExcelUtil;
import com.group02.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@CrossOrigin
@Api
@RequestMapping("/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    /**
     * 分页查询列表
     *
     * @param currentPage
     * @param pageSize
     * @param zyCommunity
     * @return
     */
    @ApiOperation("获取小区分页列表")
    @PostMapping("/communityPageList")
    public ResponseResult<IPage<ZyCommunityVO>> getCommunityList(Integer currentPage, Integer pageSize, @RequestBody ZyCommunity zyCommunity) {
        IPage<ZyCommunityVO> communityList = communityService.getCommunityList(currentPage, pageSize, zyCommunity);

        return ResponseResult.ok(communityList);
    }

    /**
     * 获取小区名称的集合
     *
     * @return
     */
    @ApiOperation("获取小区名称的集合")
    @GetMapping("/communityNameList")
    public ResponseResult<List<String>> getCommunityNameList() {
        return ResponseResult.ok(communityService.getCommunityNameList());
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation("根据数组批量删除小区")
    @PostMapping("/delByIds")
    public ResponseResult<String> deleteCommunityByIds(@RequestBody String[] ids) {

        ArrayList<String> list = new ArrayList<>(Arrays.asList(ids));
        communityService.removeBatchByIds(list);
        return ResponseResult.ok("删除成功");
    }

    /**
     * 数据导出
     *
     * @param ids
     * @param response
     * @return
     */
    @ApiOperation("更具选中的数组导出小区数据到excel")
    @PostMapping("/excel")
    public ResponseResult<String> exportCommunityByIds(@RequestBody String[] ids, HttpServletResponse response) {

        System.out.println(ids);
        ArrayList<String> list = new ArrayList<>(Arrays.asList(ids));
        List<ZyCommunity> zyCommunities = communityService.listByIds(list);
        String fileName = UUID.randomUUID().toString();
        String path = "C:\\Users\\Administrator\\Desktop\\" + fileName + "小区信息绩表.xls";    //导出路径
        EasyExcelUtil.write(path, ZyCommunity.class, zyCommunities, 0, "小区信息绩表");
        return ResponseResult.ok("导出成功");
    }

    /**
     * 添加小区
     *
     * @param zyCommunity
     * @return
     * @throws ParseException
     */
    @ApiOperation("添加小区")
    @PostMapping("/add")
    public ResponseResult<String> addCommunity(@RequestBody ZyCommunity zyCommunity) throws ParseException {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = dateFormat.format(date);

        Date time = dateFormat.parse(format);
        zyCommunity.setCreateTime(time);
        communityService.save(zyCommunity);

        return ResponseResult.ok("添加成功");

    }

    @ApiOperation("获取所有小区")
    @GetMapping("/all")
    public ResponseResult<List<ZyCommunity>> getList() {
        return ResponseResult.ok(communityService.list(null));
    }

    @ApiOperation("修改小区")
    @PutMapping("/update")
    public ResponseResult<String> updateCommunity(@RequestBody ZyCommunity zyCommunity){
        communityService.updateById(zyCommunity);

        return ResponseResult.ok("修改成功");
    }

    @PutMapping("/updateZyCommunityDept")
    public ResponseResult<String> updateZyCommunityDept(@RequestBody ZyCommunity zyCommunity) {
        ZyCommunity Community = communityService.getOne(new QueryWrapper<ZyCommunity>().lambda().eq(ZyCommunity::getCommunityId, zyCommunity.getCommunityId()));
        Community.setDeptId(zyCommunity.getDeptId());
        communityService.updateById(Community);
        return ResponseResult.ok("更换物业成功");
    }

    /**
     * 查询楼栋名字集合
     *
     * @return
     */
    @ApiOperation("查询楼栋名字集合")
    @GetMapping("/nameList")
    public ResponseResult<List<String>> getBuildingNames() {
        List<String> communityNameList = communityService.getCommunityNameList();
        return ResponseResult.ok(communityNameList);
    }

}
